<?php

/**
 * Created by PhpStorm.
 * User: yh
 * Date: 2017/6/6
 * Time: 10:44
 */
class Zone_model extends BS_Model
{
    /**
     * 根据父节点id获取所有子节点
     * 父节点id,不传则获取所有省份信息(pid=1)
     * @return array
     */
    public function getZonesByParentId($pid = 1)
    {
        $this->db->select('*');
        $this->db->from('zone');
        $this->db->where('pid', $pid);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        return $query->result_array();
    }

    /**
     * 根据节点id获取该节点的地区信息
     * @param $id
     * @return array
     */
    public function getZonesById($id)
    {
        if(!isset($id))
        {
            return array();
        }
        $this->db->select('*');
        $this->db->from('zone');
        $this->db->where('id', $id);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        return $query->row_array();
    }

    /**
     * 根据type获取该type对应的所有地区信息
     * @param $type 0:国家，1：省份，2：城市，3：地区
     * @return array
     */
    public function getZonesByType($type)
    {
        if(!isset($type))
        {
            return array();
        }
        $this->db->select('*');
        $this->db->from('zone');
        $this->db->where('type', $type);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        return $query->result_array();
    }

    public function getZonesDetailById($id){
        $data = '';
        $this->db->select('*');
        $this->db->from('zone');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $res = $query->row_array();
        if( $res['type'] > 0 ){
            $data .= $res['name'];
            $ndata = $this->getZonesDetailById($res['pid']);
            if(isset($ndata)){
                $data .= ','.$ndata;
            }
        }else{
            $data .='中国';
        }

        return $data;
    } 

    public function getZonesStringById($id){
        $res = $this->getZonesDetailById($id);
        if(isset($res)){
            $data = implode('',array_reverse(explode(',',$res)));
        }
        return $data;
    }
}